Namespacing everything to /UVa.
[and.git] / UVa / 705 - Slash maze / 705.3.cpp
blobaf0086641cf921a4ce91e621975d83cec256b316
1 #include <algorithm>
2 #include <iostream>
3 #include <map>
4 #include <vector>
5 #include <set>
7 using namespace std;
9 typedef pair<int, int> node;
10 typedef map<node, set<node> > graph;
12 int longest;
13 int qty;
15 graph g;
17 char m[75][75];
19 map<node, int> d;
21 void dfs(const node &u){
22 set<node> &vecinos = g[u];
23 for (set<node>::iterator i = vecinos.begin(); i != vecinos.end(); ++i){
24 if (d.count(*i)){
25 if (d[*i] + 1 < d[u]){
26 //printf("Cycle from (%d, %d) to (%d, %d)\n", i->first, i->second, u.first, u.second);
27 qty++;
28 longest = max(longest, d[u] - d[*i] + 1);
30 }else{
31 d[*i] = d[u] + 1;
32 dfs(*i);
37 void connect(const node &u, const node &v){
38 g[u].insert(v);
39 g[v].insert(u);
42 int main(){
43 int w,h;
44 int mazeNo = 1;
45 while (cin >> w >> h && w && h){
46 g.clear();
47 d.clear();
49 longest = -1;
50 qty = 0;
52 for (int i=0; i<w; ++i){
53 for (int j=0; j<h; ++j){
54 cin >> m[i][j];
58 for (int i=0; i<w; ++i){
59 for (int j=0; j<h; ++j){
61 //laterales
62 connect(node(i, 2*j), node(i, 2*j-1));
63 connect(node(i, 2*j+1), node(i, 2*j+2));
66 char c = m[i][j];
67 if (c == '\\'){
68 int ni, nj;
70 //arriba
71 ni = i-1;
72 nj = j;
73 if (0 <= ni && ni < w && 0 <= nj && nj < h &&
74 m[ni][nj] != m[i][j]){
76 connect(node(i, 2*j+1), node(ni, 2*nj+1));
79 //abajo
80 ni = i+1;
81 nj = j;
82 if (0 <= ni && ni < w && 0 <= nj && nj < h &&
83 m[ni][nj] != m[i][j]){
85 connect(node(i, 2*j), node(ni, 2*nj));
88 }else if (c == '/'){
89 int ni, nj;
91 //arriba
92 ni = i-1;
93 nj = j;
94 if (0 <= ni && ni < w && 0 <= nj && nj < h &&
95 m[ni][nj] != m[i][j]){
96 connect(node(i, 2*j), node(ni, 2*nj));
99 //abajo
100 ni = i+1;
101 nj = j;
102 if (0 <= ni && ni < w && 0 <= nj && nj < h &&
103 m[ni][nj] != m[i][j]){
104 connect(node(i, 2*j+1), node(ni, 2*nj+1));
108 }else{
109 cerr << "Unrecognized char in input" << endl;
114 /*for (map<node, set<node> >::iterator i = g.begin(); i != g.end(); ++i){
115 printf("Vecinos de (%d, %d):\n", i->first.first, i->first.second);
116 set<node> v = i->second;
117 for (set<node>::iterator j = v.begin(); j != v.end(); ++j){
118 printf("(%d, %d) ", j->first, j->second);
120 cout << endl;
123 for (map<node, set<node> >::iterator i = g.begin(); i != g.end(); ++i){
124 if (d.count(i->first) == 0){
125 d[i->first] = 0;
126 dfs(i->first);
130 printf("Maze #%d:\n", mazeNo++);
131 if (qty == 0){
132 printf("There are no cycles.\n");
133 }else{
134 printf("%d Cycles; the longest has length %d\n", qty, longest);
136 printf("\n");
141 return 0;